'''
Created on Feb 22, 2014

@author: yfrank
'''

import Transport.State as State

import Transport.Enums as Enums

import Transport.StateMachine as StateMachine


class Closed(State.State):

    def get_type(self):
        return Enums.PeerState.CLOSE

    def process_event(self, event, data):
        if event == Enums.PeerEvent.DISC:
            self.switch_to_next_state(self.sm.stateClosed)
            return True;

        if event == Enums.PeerEvent.CONN:
            self.switch_to_next_state(self.sm.stateOpen)
            return True;
        return False


class Open(State.State):

    def get_type(self):
        return Enums.PeerState.OPEN

    def process_event(self, event, data):
        if event == Enums.PeerEvent.DISC:
            self.switch_to_next_state(self.sm.stateClosed)
            return True;

        if event == Enums.PeerEvent.SEND:
            self.switch_to_next_state(self.sm.stateWaitPath)
            return True;
        return False

class WaitPath(State.State):

    def get_type(self):
        return Enums.PeerState.BUSY

    def process_event(self, event, data):
        if event == Enums.PeerEvent.RECV:
            if self.sm.peer.parse_path(data):
                self.switch_to_next_state(self.sm.stateOpen)
            else:
                self.switch_to_next_state(self.sm.stateWaitPath)
            return True;
        return False



class RoboticsPeerStateMacine(StateMachine.StateMachine):
    '''
    classdocs
    '''

    def __init__(self, peer):
        '''
        Constructor
        '''
        self.stateOpen = Open(self)
        self.stateClosed = Closed(self)
        self.stateWaitPath = WaitPath(self)

        super(RoboticsPeerStateMacine,self).__init__(peer,self.stateClosed)